package com.chang.rentmall.servicesImpl;

import com.chang.rentmall.beans.AddressBean;
import com.chang.rentmall.daos.AddressDao;
import com.chang.rentmall.services.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressDao addressDao;

    @Override
    public HashMap<String, Object> addAddress(AddressBean addressBean) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        int flag = addressDao.addAddress(addressBean);
        if (flag == 1) {
            result.put("code", 200);
            result.put("message", "操作成功");
            result.put("body", "");
        } else {
            result.put("code", -1);
            result.put("message", "操作失败");
            result.put("body", "");
        }
        return result;
    }

    @Override
    public HashMap<String, Object> getAddresses(int id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        List<AddressBean> list = addressDao.getAddresses(id);
        result.put("code", 200);
        result.put("message", "查询数据成功");
        result.put("list", list);
        return result;
    }

    @Override
    public HashMap<String, Object> updateAddress(AddressBean addressBean) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        int flag = addressDao.updateAddress(addressBean);
        if (flag == 1) {
            result.put("code", 200);
            result.put("body", "");
            result.put("message", "操作成功");

        } else {
            result.put("code", 200);
            result.put("body", "");
            result.put("message", "操作成功");
        }
        return result;
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @Override
    public HashMap<String, Object> deleteAddress(int id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        int flag = addressDao.deleteAddress(id);
        if (flag == 1) {
            result.put("message", "操作成功");
            result.put("code", 200);
            result.put("body", "");
        } else {
            result.put("message", "操作失败");
            result.put("code", -1);
            result.put("body", "");
        }
        return result;
    }

    @Override
    public HashMap<String, Object> getAddressDetail(int id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        AddressBean addressBean = addressDao.getAddressDetail(id);
        result.put("message", "查询成功");
        result.put("code", 200);
        result.put("body", addressBean);
        return result;
    }
}
